package com.wanghaixin.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.wanghaixin.dao.ProductCarMapper;
import com.wanghaixin.dao.ProductMapper;
import com.wanghaixin.dto.DataGridResult;
import com.wanghaixin.dto.QueryDTO;
import com.wanghaixin.pojo.ProductCar;
import com.wanghaixin.pojo.ProductCarExample;
import com.wanghaixin.pojo.ProductExample;
import com.wanghaixin.pojo.ProductWithBLOBs;
import com.wanghaixin.service.ProductCarService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;

/**
 * @Auther: 王海新
 * @Date: 2020/11/2 20:21
 * @Description:  使用还有文本的实体类的时候，调用example的发生改变。后面加WithBLOBs
 * 分别是 修改和 分页功能
 */
@Service
public class ProductcarServiceImpl implements ProductCarService {

    @Autowired
    private ProductMapper productMapper;
    @Override
    public int addProductCar(ProductWithBLOBs productWithBLOBs) {
        int i = productMapper.insertSelective(productWithBLOBs);
        return i;
    }

    @Override
    public void delProductCar(Integer id) {
        int i = productMapper.deleteByPrimaryKey(id);
    }

    @Override
    public int upProductCar(ProductWithBLOBs productWithBLOBs) {
        //修改方法调用发生改变
        int update = productMapper.updateByPrimaryKeyWithBLOBs(productWithBLOBs);
        return update;
    }

    @Override
    public ProductWithBLOBs findById(Integer id) {
        ProductWithBLOBs productWithBLOBs = productMapper.selectByPrimaryKey(id);
        return productWithBLOBs;
    }

    @Override
    public DataGridResult findByPage(QueryDTO queryDTO) {
        PageHelper.offsetPage(queryDTO.getOffset(),queryDTO.getLimit());
        ProductExample example = new ProductExample();
        if (!StringUtils.isEmpty(queryDTO.getSort())) {
            example.setOrderByClause("id");
        }
        //分页调用发生改变
        List<ProductWithBLOBs> productWithBLOBs = productMapper.selectByExampleWithBLOBs(example);
        PageInfo<ProductWithBLOBs> carPageInfo = new PageInfo<>(productWithBLOBs);
        long total = carPageInfo.getTotal();
        DataGridResult dataGridResult = new DataGridResult(total, productWithBLOBs);
        return dataGridResult;
    }}